// pages/isnotcorrect/isnotcorrect.js
let conf = require('../../app.config').app
const { ScanQRcode , Qrcodelength } = require('../../common/js/utils')
Page({

  /**
   * 页面的初始数据
   */
  data: {
      // 活力值码
      code: '',
      message: '',
      pinCode: '',
      cdn: conf.cdnPath
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({
        code: options.code,
        alert: that.selectComponent("#alert"),
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.removeStorage({
      key: 'scancode',
      success: function(res) {
      },
    })
  },
  travel() {
    wx.reLaunch({
        url: '../home/home',
    });
  },
  // 提交
  onSubmit(that) {
    // 请求接口提交
    this.api.interface.smallScanPoint({
      pinCode:that.data.pinCode
    }).then(result=>{
      ScanQRcode(result)
    })
  },
  // 扫一扫
  onScanTap(event) {
    const that = this;
    wx.requestSubscribeMessage({
        tmplIds: ['QVk5ycLHaIqtUSN651qTuL13SxXfTIIA6ek7z5cBA8E'],
        complete() {
            wx.scanCode({
                onlyFromCamera: true,
                success(res) {
                    var qrcode = decodeURIComponent(res.result).replace(/\s+/g,"").replace(/[\r\n]/g,"");
                    var code = ''
                    if (qrcode.length > 43) {
                        qrcode = qrcode.substring(0,43)
                    } else if (qrcode.length > 38 && qrcode.length < 43) {
                        qrcode = qrcode.substring(0,38)
                    }
                    // app.alert(qrcode)
                    // console.log('错误页面的码', qrcode)
                    if (38 == qrcode.length)
                        code = qrcode.substring(26);
                    else if (43 == qrcode.length)
                        code = qrcode.substring(27);

                    // console.log('错误页面的二维码码', code)
                    that.setData({ pinCode: code });
                    that.onSubmit(that);
                }
            });

        }
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})